var express = require('express');
var router = express.Router();
var uuid = require('uuid').v1;

router.post('/user/login', async function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;
    var Sql = `SELECT * FROM user WHERE username = ? AND password = ?`;
    try {
        var vals = await asyncQuery(Sql, [username, password]);
        if (vals.length == 1) {
            delete vals[0].password;
            delete vals[0].disable;
            delete vals[0].createTime;
            delete vals[0].updateTime;
            res.json(vals[0]);
        } else if (vals.length == 0) {
            res.status(500);
            res.json({
                msg: '用户名或密码错误'
            });
        } else {
            res.status(500);
            res.json({
                msg: '未知错误，请联系管理员'
            });
        };
    } catch (error) {
        next(error)
    };
});

router.post('/user/register', async function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;
    var nickname = req.body.nickname;
    var gender = req.body.gender;
    var userId = uuid();
    var Sql = `INSERT INTO user SET ?`;
    var searchSql = `SELECT * FROM user WHERE username = ?`;
    try {
        var user = await asyncQuery(searchSql, [username]);
        if (user.length >= 1) {
            res.status(500);
            res.json({
                msg: '用户名已经存在，请重试'
            });
        } else {
            await asyncQuery(Sql, {
                user_id: userId,
                gender: gender,
                avatar: '000',
                username: username,
                password: password,
                nickname: nickname,
                disable: 0,
            });
            res.json({
                id: userId
            });
        };
    } catch (error) {
        next(error)
    };
});

router.put('/user/updateMessage', async function (req, res, next) {
    var userId = req.headers.token;
    var userSql = `UPDATE user SET ? WHERE user_id = ?`;
    try {
        await asyncQuery(userSql, [{
            nickname: req.body.nickname,
            gender: req.body.gender,
            avatar: req.body.avatar
        }, userId]);
        res.json({});
    } catch (error) {
        next(error)
    };
});


router.post('/project/searchUser', async function (req, res, next) {
    var username = req.body.username;
    var userSql = `SELECT * FROM user WHERE username = ?`;
    try {
        var vals = await asyncQuery(userSql, [username]);
        if (vals.length == 1) {
            delete vals[0].password;
            delete vals[0].disable;
            delete vals[0].createTime;
            delete vals[0].updateTime;
            res.json(vals[0]);
        } else {
            res.status(500);
            res.json({
                msg: '未找到用户'
            });
        };
    } catch (error) {
        next(error)
    };
});


router.post('/project/addUser', async function (req, res, next) {
    var projectId = req.query.projectId;
    var permission = req.body.permission;
    var userid = req.body.userId;
    var token = req.headers.token;
    var projectUserId = uuid();

    var searchProjectUser = `SELECT * FROM project_user WHERE project_id = ? AND user_id = ?`;
    var addSql = `INSERT INTO project_user SET ?`;

    try {
        var value = await Permission.CPP(token, projectId);
        if (value == 'admin') {
            var vals = await asyncQuery(searchProjectUser, [projectId, userid]);
            if (vals.length == 0) {
                await asyncQuery(addSql, {
                    project_user_id: projectUserId,
                    project_id: projectId,
                    user_id: userid,
                    permission: permission,
                });
                res.json({
                    projectUserId: projectUserId
                });
            } else {
                res.status(500);
                res.json({
                    msg: '用户已经在项目中'
                });
            };
        };
    } catch (error) {
        next(error)
    };

});




module.exports = router;